package org.webshop.backend.dbchif.sql;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.Statement;

/**
 * 
 * @author Thomas Jahoda
 *         Defines SQL Statements with metadata
 *         The actual SQL is written in DbAccessStatements.sql.properties under the resources path
 */
public enum DbAccessStatement implements RawStatement {

    ADD_INGREDIGENT("ingredient.add", true),
    REMOVE_INGREDIENT("ingredient.remove"),
    EDIT_INGREDIENT("ingredient.edit"),
    GET_INGREDIENTS("ingredient.all"),
    GET_INGREDIENTS_SORTED_BY_ID("ingredient.all.sortedbyid"),
    GET_INGREDIENT("ingredient.single"),
    SELECT_PRODUCT("product.all"),
    SELECT_RESTAURANT_DESIGNATION("restaurant.designations"),
    ADD_PRODUCT("product.add", true),
    ADD_INGREDIENT_TO_PRODUCT("product.addIngredient");

    private String sql;
    private boolean generatedKeysEnabled;

    private DbAccessStatement(String sql) {
        this(sql, false);
    }

    /**
     * @param sql
     * @param generatedKeysEnabled for insert statements to take a look at the generated data from the db
     */
    // private DbAccessStatement(String sql, boolean generatedKeysEnabled, int directSqlDummy) {
    // this.sql = sql;
    // this.generatedKeysEnabled = generatedKeysEnabled;
    // }
    /**
     * 
     * @param sqlPropertyIdentifier the sql property name
     * @param generatedKeysEnabled for insert statements to take a look at the generated data from the db
     */
    private DbAccessStatement(String sqlPropertyIdentifier, boolean generatedKeysEnabled) {
        String sqlStatement = SqlPropertiesLoader.getSqlStatement(sqlPropertyIdentifier);
        this.sql = sqlStatement;
        this.generatedKeysEnabled = generatedKeysEnabled;
    }

    @Override
    public String getSql() {
        return sql;
    }

    @Override
    public int getIdentifier() {
        return ordinal();
    }

    public boolean isGeneratedKeysEnabled() {
        return generatedKeysEnabled;
    }

    @Override
    public PreparedStatement prepareOn(Connection con) throws SQLException {
        return prepareStatement(con, getIdentifier(), getSql(), isGeneratedKeysEnabled());
    }

    private static PreparedStatement prepareStatement(Connection con, Integer key, String sql,
            boolean generatedKeysEnabled) throws SQLException {
        int generatedKeysState = generatedKeysEnabled ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS;
        PreparedStatement statement = con.prepareStatement(sql, generatedKeysState);
        return statement;
    }

}

class SqlPropertiesLoader {

    private static final Properties SQL_STATEMENTS;
    static {
        Properties sql = new Properties();
        try {
            sql.load(DbAccessStatement.class.getClassLoader().getResourceAsStream("DbAccessStatements.sql.properties"));
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e); // "could not load properties file for sql statements",
        }
        SQL_STATEMENTS = sql;
    }

    static String getSqlStatement(String name) {
        return SQL_STATEMENTS.getProperty(name);
    }

}
